package com.geeksaga.advice;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.geeksaga.domain.User;
import com.geeksaga.message.GeekSagaMessage;
import com.geeksaga.message.MailMessage;
import com.geeksaga.service.SendMailService;

/**
 * @author newbie
 * @since 0.1
 */
@Aspect
@Component
public class NotificationAspect {
    protected final Log logger = LogFactory.getLog(NotificationAspect.class);
    
    @Autowired private SendMailService sendMailService = null;

    @Pointcut("execution(* * ..IssueService.add(..))")
    public void notifyObserver() {
        logger.info("BBBB");
        logger.info("BBBB");
    }

    @Before("notifyObserver()")
    public void takeSeats() {
        logger.info("The audience is taking their seats.");
    }

    @Before("notifyObserver()")
    public void turnOffCellPhones() {
        logger.info("The audience is turning off their cellphones");
    }

    @AfterReturning("notifyObserver()")
    public void sendMail() {
        User user = new User();
        user.setEmail("smileemaker@gmail.com");
        
        GeekSagaMessage message = new MailMessage(user);
//        sendMailService.sendMail(message);
        logger.info("sendMail()");
    }

    @AfterThrowing("notifyObserver()")
    public void demandRefund() {
        logger.info("Boo! We want our money back!");
    }
}